﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using IniParser;
using IniParser.Model;
using System.Net;
using System.Net.Mail;
using System.Net.Mime;

namespace ChatBuddy.Forms.Secondary.EmailWindow
{
    public partial class EmailWindow : Form
    {
        private FileIniDataParser _dataParser = new FileIniDataParser();
        private IniData _parser = new IniData();

        public EmailWindow()
        {
            InitializeComponent();
        }

        private void EmailWindow_Load(object sender, EventArgs e)
        {
            Icon = new Icon(Path.Combine(Application.StartupPath, @"plugins\ChatBuddy\bin\img\email.ico"));

            _dataParser = new FileIniDataParser();
            _parser = _dataParser.ReadFile(Path.Combine(Application.StartupPath, @"plugins\ChatBuddy\bin\Settings\chatbuddy.ini"));

            tbUser.Text = _parser["Email"]["User"];
            tbPassword.Text = _parser["Email"]["Password"];
            tbHost.Text = _parser["Email"]["Host"];
            tbPort.Text = _parser["Email"]["Port"];

            nudInterval.Value = Convert.ToDecimal(_parser["Email"]["Interval"]);
            cbSend.Checked = Convert.ToBoolean(_parser["Email"]["UseNotification"]);
            cbSsl.Checked = Convert.ToBoolean(_parser["Email"]["SSL"]);

            SelectType(Convert.ToInt32(_parser["Email"]["Type"]));
        }

        private void SelectType(int type)
        {
            switch (type)
            {
                case 1:
                    rbWhisper.Checked = true;
                    break;
                case 2:
                    rbSaid.Checked = true;
                    break;
                case 3:
                    rbFull.Checked = true;
                    break;
            }
        }

        private void btnTestMessage_Click(object sender, EventArgs e)
        {
            try
            {
                var client = new SmtpClient(tbHost.Text) {UseDefaultCredentials = false};

                var authenticationInfo = new NetworkCredential(tbUser.Text, tbPassword.Text);
                client.Credentials = authenticationInfo;
                client.Port = Convert.ToInt32(tbPort.Text);
                client.EnableSsl = true;
                client.DeliveryMethod = SmtpDeliveryMethod.Network;

                var from = new MailAddress("no-reply@honorbuddy.com");
                var to = new MailAddress(tbUser.Text);

                var message = new MailMessage(from, to)
                {
                    Subject = "Testing",
                    SubjectEncoding = System.Text.Encoding.UTF8,
                    Body = "This is a test email from ChatBuddy.",
                    BodyEncoding = System.Text.Encoding.UTF8,
                    IsBodyHtml = false
                };

                client.Send(message);
            }
            catch (Exception er)
            {
                MessageBox.Show("There was a problem sending an email, the problem returned is as follows: \n\n" + er.Message);
            }
        }

        private void btnApply_Click(object sender, EventArgs e)
        {
            _parser["Email"]["User"] = tbUser.Text;
            _parser["Email"]["Password"] = tbPassword.Text;
            _parser["Email"]["Host"] = tbHost.Text;
            _parser["Email"]["Port"] = tbPort.Text;

            if (rbWhisper.Checked)
                _parser["Email"]["Type"] = "1";
            if (rbSaid.Checked)
                _parser["Email"]["Type"] = "2";
            if (rbFull.Checked)
                _parser["Email"]["Type"] = "3";

            _parser["Email"]["UseNotification"] = cbSend.Checked.ToString();
            _parser["Email"]["Interval"] = nudInterval.Value.ToString();
            _parser["Email"]["SSL"] = cbSsl.Checked.ToString();
            _dataParser.SaveFile(Path.Combine(Application.StartupPath, @"plugins\ChatBuddy\bin\Settings\chatbuddy.ini"), _parser);
        }
    }
}
